package mspbots.data.tl.prod.autotask.service.impl;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultEntity;
import mspbots.data.tl.prod.autotask.mapper.TaskAutoContractMapper;
import mspbots.data.tl.prod.autotask.mapper.TaskAutotaskCompanyMapper;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskCompany;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskContact;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskContract;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskContractService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
@Log4j2
@RequiredArgsConstructor
@DS("master")
public class TaskAutotaskContractServiceImpl  extends AbstractBaseServiceImpl<TaskAutoContractMapper, TaskAutotaskContract>
        implements TaskAutotaskContractService {


    private final TaskAutoContractMapper taskAutoContractMapper;

    @Override
    public void sync(ResultEntity content) {
        log.debug("Auto task contract receiver content {}", content);
        JsonNode entity = content.getEntity();
        TaskAutotaskContract data = TaskAutotaskContract.builder()
                .contractId(super.getLongValue(entity.get("id")))
                .status(ObjectUtils.isEmpty(entity.get("status")) ? null : entity.get("status").asText())
                .billingPreference(ObjectUtils.isEmpty(entity.get("billingPreference")) ? null : entity.get("billingPreference").asText())
                .contractCategory(ObjectUtils.isEmpty(entity.get("contractCategory")) ? null : entity.get("contractCategory").asText())
                .contractType(ObjectUtils.isEmpty(entity.get("contractType")) ? null : entity.get("contractType").asText())
                .contractName(ObjectUtils.isEmpty(entity.get("contractName")) ? null : entity.get("contractName").asText())
                .estimatedRevenue(ObjectUtils.isEmpty(entity.get("estimatedRevenue")) ? null : entity.get("estimatedRevenue").asDouble())
                .overageBillingRate(ObjectUtils.isEmpty(entity.get("overageBillingRate")) ? null : entity.get("overageBillingRate").asDouble())
                .endDate(super.getDateTimeValue(entity.get("endDate")))
                .purchaseOrderNumber(ObjectUtils.isEmpty(entity.get("purchaseOrderNumber")) ? null : entity.get("purchaseOrderNumber").asText())
                .estimatedCost(ObjectUtils.isEmpty(entity.get("estimatedCost")) ? null : entity.get("estimatedCost").asDouble())
                .isCompliant(ObjectUtils.isEmpty(entity.get("isCompliant")) ? null : entity.get("isCompliant").asBoolean())
                .isDefaultContract(ObjectUtils.isEmpty(entity.get("isDefaultContract")) ? null : entity.get("isDefaultContract").asBoolean())
                .contractNumber(ObjectUtils.isEmpty(entity.get("contractNumber")) ? null : entity.get("contractNumber").asText())
                .setupFee(ObjectUtils.isEmpty(entity.get("setupFee")) ? null : entity.get("setupFee").asDouble())
                .startDate(super.getDateTimeValue(entity.get("startDate")))
                .estimatedHours(ObjectUtils.isEmpty(entity.get("estimatedHours")) ? null : entity.get("estimatedHours").asText())
                .timeReportingRequiresStartAndStopTimes(ObjectUtils.isEmpty(entity.get("timeReportingRequiresStartAndStopTimes")) ? null : entity.get("timeReportingRequiresStartAndStopTimes").asDouble())
                .description(ObjectUtils.isEmpty(entity.get("description")) || "null".equalsIgnoreCase(entity.get("description").asText()) ? null : entity.get("description").asText())
                .syncTime(LocalDateTime.now())
                .build();
        data.setTenantCode(content.getTenantId());


        TaskAutotaskContract dbData = super.baseMapper.selectOne(Wrappers.<TaskAutotaskContract>lambdaQuery()
                .eq(TaskAutotaskContract::getTenantCode, data.getTenantCode())
                .eq(TaskAutotaskContract::getContractId, data.getContractId())
                .last("limit 1"));
        if (ObjectUtils.isEmpty(dbData)) {
            super.baseMapper.insert(data);
        } else {
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }
    }
}
